/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.pubsub;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.edi.Kola;
import com.edi.client.param.EdiClientParam;
import com.edi.client.param.EdiServerParam;
import com.edi.client.plugin.Plugin;
import com.edi.client.plugin.PluginResult;
import com.edi.manager.PluginManager;

public class LocalService extends EdiService {
	private static final Logger log = Logger.getLogger(LocalService.class);

	public LocalService(String serviceName) {
		super(serviceName);

	}

	/**
	 * 本地服务执行动作
	 */
	public PluginResult execute(EdiClientParam clientParam) throws Exception {
		PluginManager pluginManager = Kola.getPluginManager();
		Plugin plugin = null;
		if (!StringUtils.contains(serviceName, ":")) {// 脚本型服务
			log.debug(" serviceName =" + serviceName);
			plugin = pluginManager.getScriptPluginByName(StringUtils
					.substringBefore(serviceName, "."));
		} else// if(StringUtils.contains(serviceName, ":"))
		{
			String className = StringUtils.substringBetween(serviceName, ":");
			log.debug("className =" + className);
			plugin = pluginManager.getScriptPluginByName(className);
		}
		log.debug(" plugin == null " + (plugin == null));
		if (plugin == null)
			return null;
		log.debug(" plugin =" + (plugin.getClass()));
		PluginResult result = plugin.invoke(clientParam);
		return result;
	}

	public PluginResult execute(EdiServerParam serverParam) throws Exception {
		PluginManager pluginManager = Kola.getPluginManager();
		Plugin plugin = null;
		if (!StringUtils.contains(serviceName, ":")) {// 脚本型服务
			log.debug(" serviceName =" + serviceName);
			plugin = pluginManager.getScriptPluginByName(StringUtils
					.substringBefore(serviceName, "."));
		} else {
			String className = StringUtils.substringBetween(serviceName, ":");
			log.debug("className =" + className);
			plugin = pluginManager.getScriptPluginByName(className);
		}
		if (plugin == null)
			return null;
		log.debug(" plugin =" + (plugin.getClass()));
		PluginResult result = plugin.invoke(serverParam);
		return result;
	}

}
